/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DisplayViewPanel.java
 *
 * Created on Jun 14, 2011, 2:27:54 AM
 */
package rmiserver.ui.view;

import java.beans.PropertyChangeEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import rmi.common.ui.view.AbstractViewPanel;
import rmiserver.service.DefaultFileSharing;
import rmiserver.ui.controller.DefaultController;
import rmiserver.ui.model.ConnectionBean;

/**
 *
 * @author hienpham
 */
public class DisplayViewPanel extends AbstractViewPanel {

	private DefaultController controller;
    /** Creates new form DisplayViewPanel */
    public DisplayViewPanel(DefaultController controller) {
    	this.controller = controller;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableConnection = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tableLogin = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        tbSharedFile = new javax.swing.JTable();
        btnShareFile = new javax.swing.JButton();

        tableConnection.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Time", "Client Name"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tableConnection.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(tableConnection);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 645, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 466, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("Connection Statistic", jPanel1);

        tableLogin.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Time", "Account"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tableLogin.getTableHeader().setReorderingAllowed(false);
        jScrollPane2.setViewportView(tableLogin);
        tableLogin.getColumnModel().getColumn(0).setResizable(false);
        tableLogin.getColumnModel().getColumn(0).setPreferredWidth(4);
        tableLogin.getColumnModel().getColumn(1).setResizable(false);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 645, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 466, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("Login Statistic", jPanel2);

        tbSharedFile.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"File 1", null},
                {"File 2", null},
                {"File 3", null},
                {"File 4", null},
                {"File 5", null},
                {"File 6", null},
                {"FIle 7", null},
                {"FIle 8", null},
                {"File 9", null},
                {"File 10", null},
                {"File 11", null},
                {"File 12", null},
                {"File 13", null},
                {"File 14", null},
                {"File 15", null},
                {"File 16", null},
                {"File 17", null},
                {"FIle 18", null},
                {"File 19", null},
                {"File 20", null}
            },
            new String [] {
                "File name", "Share"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Boolean.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        tbSharedFile.getTableHeader().setReorderingAllowed(false);
        jScrollPane3.setViewportView(tbSharedFile);
        tbSharedFile.getColumnModel().getColumn(0).setResizable(false);
        tbSharedFile.getColumnModel().getColumn(1).setResizable(false);

        btnShareFile.setText("Share selected files");
        btnShareFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnShareFileActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 645, Short.MAX_VALUE)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(243, 243, 243)
                .addComponent(btnShareFile)
                .addContainerGap(268, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 302, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnShareFile)
                .addContainerGap(124, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("File Setting", jPanel3);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 653, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 502, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnShareFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnShareFileActionPerformed
    	TableModel model = tbSharedFile.getModel();
    	List<String> files = new ArrayList<String>();
    	for (int i = 0; i < model.getRowCount() - 1; i ++) {
    		if (model.getValueAt(i, 1) != null && model.getValueAt(i, 1).toString().equals("true")) {
    			files.add(model.getValueAt(i, 0).toString());
    		}
    	}
    	
    	DefaultFileSharing fileSharing = DefaultFileSharing.getInstance();
    	fileSharing.setSharedFiles(files);
    }//GEN-LAST:event_btnShareFileActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnShareFile;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable tableConnection;
    private javax.swing.JTable tableLogin;
    private javax.swing.JTable tbSharedFile;
    // End of variables declaration//GEN-END:variables

    @Override
    public void modelPropertyChange(PropertyChangeEvent evt) {
    	if (evt.getPropertyName().equals(DefaultController.ELEMENT_LIST_USER)) {
    		DefaultTableModel model = (DefaultTableModel) tableConnection.getModel();
    		ConnectionBean bean = (ConnectionBean) evt.getNewValue();
    		model.addRow(new Object[] {bean.getTimeStamp(), bean.getHost()});
    	} else if (evt.getPropertyName().equals(DefaultController.ELEMENT_LOGGED_USER)) {
    		DefaultTableModel model = (DefaultTableModel) tableLogin.getModel();
    		ConnectionBean bean = (ConnectionBean) evt.getNewValue();
    		model.addRow(new Object[] {bean.getTimeStamp(), bean.getUsername() + " [ IP : " + bean.getHost() + " ]"});
    	}
    }
}
